package com.burton.dp.nolock;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;

public class NoLockDemo {

	static String path = "/lock_path";
	static CuratorFramework client = CuratorFrameworkFactory.builder()
			.connectString("127.0.0.1:2181").sessionTimeoutMs(5000)
			.retryPolicy(new ExponentialBackoffRetry(1000, 3)).build();

	public static void main(String[] args) throws Exception {
		client.start();

		final InterProcessMutex lock = new InterProcessMutex(client, path);
//		final CountDownLatch down = new CountDownLatch(1);
		for (int i = 0; i < 38; i++) {
			new Thread(new Runnable() {

				public void run() {
					try {
//						down.await();
						lock.acquire();
					} catch (Exception e) {
					}
					SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss|SSS");
					String orderNo =df.format(new Date());
					System.out.println("订单号："+orderNo);
					try {
						lock.release();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}).start();
		}
		
//		down.countDown();
	}

}
